{% extends 'base.html' %}
{% load static %}

{% block title %}逾期借用提醒{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">
                        <i class="fas fa-exclamation-triangle text-warning me-2"></i>逾期借用提醒
                        {% if page_obj %}
                            <span class="badge bg-warning ms-2">{{ page_obj.paginator.count }} 项</span>
                        {% endif %}
                    </h5>
                </div>
                
                <div class="card-body">
                    {% if page_obj %}
                        <div class="alert alert-warning">
                            <h6><i class="fas fa-info-circle me-2"></i>提醒说明</h6>
                            <ul class="mb-0">
                                <li>以下资产借用已超过预定归还时间，请及时联系借用人办理归还手续</li>
                                <li>逾期时间越长，风险越高，建议优先处理逾期时间较长的项目</li>
                                <li>可点击"查看详情"联系借用人或直接办理归还手续</li>
                            </ul>
                        </div>

                        <div class="table-responsive">
                            <table class="table table-striped">
                                <thead class="table-dark">
                                    <tr>
                                        <th>资产信息</th>
                                        <th>借用人</th>
                                        <th>借用目的</th>
                                        <th>借出时间</th>
                                        <th>预计归还</th>
                                        <th>逾期天数</th>
                                        <th>联系方式</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for borrow in page_obj %}
                                        <tr {% if borrow.days_overdue > 7 %}class="table-danger"{% elif borrow.days_overdue > 3 %}class="table-warning"{% endif %}>
                                            <td>
                                                <strong>{{ borrow.asset.name }}</strong><br>
                                                <small class="text-muted">{{ borrow.asset.asset_number }}</small><br>
                                                <small class="text-muted">{{ borrow.asset.category.name }}</small>
                                            </td>
                                            <td>
                                                <strong>{{ borrow.borrower.get_full_name|default:borrow.borrower.username }}</strong><br>
                                                <small class="text-muted">{{ borrow.borrower_department.name|default:"未分配部门" }}</small>
                                            </td>
                                            <td>
                                                <span class="badge bg-info">{{ borrow.get_purpose_display }}</span><br>
                                                <small class="text-muted">{{ borrow.purpose_description|truncatechars:30 }}</small>
                                            </td>
                                            <td>
                                                {{ borrow.borrowed_at|date:"Y-m-d" }}<br>
                                                <small class="text-muted">{{ borrow.borrowed_at|date:"H:i" }}</small>
                                            </td>
                                            <td>
                                                {{ borrow.expected_return_date|date:"Y-m-d" }}<br>
                                                <small class="text-muted">{{ borrow.expected_return_date|date:"H:i" }}</small>
                                            </td>
                                            <td>
                                                {% if borrow.days_overdue > 7 %}
                                                    <span class="badge bg-danger fs-6">{{ borrow.days_overdue }} 天</span>
                                                {% elif borrow.days_overdue > 3 %}
                                                    <span class="badge bg-warning fs-6">{{ borrow.days_overdue }} 天</span>
                                                {% else %}
                                                    <span class="badge bg-secondary fs-6">{{ borrow.days_overdue }} 天</span>
                                                {% endif %}
                                            </td>
                                            <td>
                                                {% if borrow.contact_phone %}
                                                    <i class="fas fa-phone me-1"></i>{{ borrow.contact_phone }}<br>
                                                {% endif %}
                                                {% if borrow.contact_email %}
                                                    <i class="fas fa-envelope me-1"></i>{{ borrow.contact_email|truncatechars:20 }}
                                                {% endif %}
                                                {% if not borrow.contact_phone and not borrow.contact_email %}
                                                    <small class="text-muted">无联系方式</small>
                                                {% endif %}
                                            </td>
                                            <td>
                                                <div class="btn-group-vertical btn-group-sm" role="group">
                                                    <a href="{% url 'assets:borrow_request_detail' borrow.pk %}" 
                                                       class="btn btn-outline-primary btn-sm">
                                                        <i class="fas fa-eye me-1"></i>查看详情
                                                    </a>
                                                    {% if user.is_staff %}
                                                        <a href="{% url 'assets:return_asset' borrow.pk %}" 
                                                           class="btn btn-outline-success btn-sm">
                                                            <i class="fas fa-arrow-left me-1"></i>办理归还
                                                        </a>
                                                    {% endif %}
                                                </div>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>

                        <!-- 分页 -->
                        {% if page_obj.has_other_pages %}
                            <nav aria-label="逾期借用分页">
                                <ul class="pagination justify-content-center">
                                    {% if page_obj.has_previous %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page=1">首页</a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}">上一页</a>
                                        </li>
                                    {% endif %}

                                    <li class="page-item active">
                                        <span class="page-link">第 {{ page_obj.number }} 页，共 {{ page_obj.paginator.num_pages }} 页</span>
                                    </li>

                                    {% if page_obj.has_next %}
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.next_page_number }}">下一页</a>
                                        </li>
                                        <li class="page-item">
                                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">末页</a>
                                        </li>
                                    {% endif %}
                                </ul>
                            </nav>
                        {% endif %}

                        <!-- 统计信息 -->
                        <div class="row mt-4">
                            <div class="col-md-4">
                                <div class="card bg-light">
                                    <div class="card-body text-center">
                                        <h5 class="card-title text-secondary">总逾期数</h5>
                                        <h3 class="text-warning">{{ page_obj.paginator.count }}</h3>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="card bg-light">
                                    <div class="card-body text-center">
                                        <h5 class="card-title text-secondary">严重逾期</h5>
                                        <h3 class="text-danger">
                                            {% with serious_count=page_obj|length %}
                                                {% for borrow in page_obj %}
                                                    {% if borrow.days_overdue > 7 %}{{ forloop.counter0|add:1 }}{% endif %}
                                                {% empty %}0{% endfor %}
                                            {% endwith %}
                                        </h3>
                                        <small class="text-muted">超过7天</small>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="card bg-light">
                                    <div class="card-body text-center">
                                        <h5 class="card-title text-secondary">一般逾期</h5>
                                        <h3 class="text-warning">
                                            {% with normal_count=page_obj|length %}
                                                {% for borrow in page_obj %}
                                                    {% if borrow.days_overdue <= 7 %}{{ forloop.counter0|add:1 }}{% endif %}
                                                {% empty %}0{% endfor %}
                                            {% endwith %}
                                        </h3>
                                        <small class="text-muted">1-7天</small>
                                    </div>
                                </div>
                            </div>
                        </div>
                    {% else %}
                        <div class="text-center py-5">
                            <i class="fas fa-check-circle fa-3x text-success mb-3"></i>
                            <h5 class="text-success">暂无逾期借用</h5>
                            <p class="text-muted">所有借用资产都在正常期限内，继续保持！</p>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
